package com.yyk.yykywd.ui.work;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseActivity;
import com.yyk.yykywd.base.BaseRecyclerAdapter;
import com.yyk.yykywd.base.BaseRecyclerHolder;
import com.yyk.yykywd.config.Constant;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.bean.WorkPlandetailInfo;
import com.yyk.yykywd.bean.WorkVisitInfo;
import com.yyk.yykywd.listenter.DialogSureCancelListenter;
import com.yyk.yykywd.util.DialogUtil;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.OnClickFlagUtil;
import com.yyk.yykywd.util.ToastUtil;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CustomerCall2Activity extends BaseActivity {
    public static CustomerCall2Activity intance;

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_postname)
    EditText etPostname;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_describe)
    EditText etDescribe;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_createtime)
    TextView tvCreatetime;
    @BindView(R.id.tv_topname)
    TextView tvTopname;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.bt_nohezuo)
    Button btNohezuo;
    @BindView(R.id.bt_yeshezuo)
    Button btYeshezuo;
    @BindView(R.id.tv_time_end)
    TextView tvTimeEnd;
    @BindView(R.id.tv_goal)
    TextView tvGoal;
    @BindView(R.id.tv_rests)
    TextView tvRests;
    @BindView(R.id.rv_udate)
    RecyclerView rvUdate;
    @BindView(R.id.iv_qbzk)
    ImageView ivQbzk;
    private String id; //业务ID

    @Override
    public int getLayoutId() {
        return R.layout.activity_customer_call2;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("客户拜访");
    }

    @Override
    public void initData() {
        intance = this;
        initGetIntent();
        postWorkPlandetail(true);

    }

    private void initGetIntent() {
        if (getBundle() != null) {
            id = getBundle().getString("id");


        }
    }

    /**
     * 接口11：客户拜访 有合作意向  暂无合作意向（whm）
     */
    private void postWorkPlanlist(final String state) {
        if (TextUtils.isEmpty(etName.getText())) {
            ToastUtil.showShort("请填写机构联系人姓名");
            return;
        }
        if (TextUtils.isEmpty(etPostname.getText())) {
            ToastUtil.showShort("请填写机构职位");
            return;
        }
        if (TextUtils.isEmpty(etPhone.getText())) {
            ToastUtil.showShort("请填写电话");
            return;
        }
        if (TextUtils.isEmpty(etDescribe.getText())) {
            ToastUtil.showShort("请填写需求描述");
            return;
        }

        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("bid", id + "");
        map.put("name", etName.getText().toString().trim() + "");
        map.put("post_name", etPostname.getText().toString().trim() + "");
        map.put("phone", etPhone.getText().toString().trim() + "");
        map.put("describe", etDescribe.getText().toString().trim() + "");
        map.put("remark", etRemark.getText().toString().trim() + "");
        map.put("state", state + ""); //1.有合作意向 2.无合作意向
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkVisit(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkVisitInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WorkVisitInfo info) {
                        Logger.e("客户拜访 有无意向" + info.toString());

                        if (info.getCode() == 1) {
                            if (TextUtils.equals(state, "1")) {
//                                ToastUtil.showShort(info.getMsg());
                                Bundle bundle = new Bundle();
                                bundle.putString("id", id);
                                //区分  是否清除缓存数据
                                bundle.putBoolean("backflag", false);
                                startAct(CustomerCall3Activity.class, false, bundle);

                            } else if (TextUtils.equals(state, "2")) {
                                DialogUtil.showReturnTips(mActivity, "", "您可继续跟进此业务，可在“暂无意向模块中跟进此业务"
                                       , "好的", "", new DialogSureCancelListenter() {
                                    @Override
                                    public void onSureListenter() {
                                        if (CustomerCallActivity.intance != null) {
                                            CustomerCallActivity.intance.finish();
                                        }
                                        Bundle bundle = new Bundle();
                                        bundle.putString("id", id);

                                        bundle.putString("intentflag", 10 + "");
                                        startAct(NoHezuoActivity.class, true, bundle);
                                    }

                                    @Override
                                    public void onCancelListenter() {

                                    }
                                }, true);


                            }

                        } else {
                            ToastUtil.showShort(info.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 接口4：查看业务详情（whm）
     *
     * @param flag
     */
    private void postWorkPlandetail(final boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("uid", Hawk.get("uid")+"");
        map.put("bid", id + "");
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postWorkPlandetail(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<WorkPlandetailInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(WorkPlandetailInfo info) {
                        Logger.e("查看业务详情" + info.toString());
                        if (info.getCode() == 1) {
                            WorkPlandetailInfo.DataBean.PlanBean plan = info.getData().getPlan();
                            //业务编号
                            String number = plan.getNumber();
                            //创建时间
                            String createtime = plan.getCreatetime();
                            //业务类型
                            String topname = plan.getTopname();
                            //机构名称
                            String name = plan.getName();
                            tvCreatetime.setText(createtime);
                            tvGoal.setText(plan.getGoal());
                            tvNumber.setText(number);
                            tvRests.setText(plan.getRests());
                            if (plan.getEnd() == 1) {
                                tvTimeEnd.setText(plan.getTime() + "   上午");
                            } else if (plan.getEnd() == 2) {
                                tvTimeEnd.setText(plan.getTime() + "   下午");
                            } else {
                                tvTimeEnd.setText(plan.getTime() + "   晚上");
                            }
                            tvTopname.setText(topname);
                            tvName.setText(name);
                            List<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> udata = plan.getUdata();
                            BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean> adapter = new BaseRecyclerAdapter<WorkPlandetailInfo.DataBean.PlanBean.UdataBean>(mActivity, udata, R.layout.adapter_item_businessplan_deteail) {
                                @Override
                                public void convert(BaseRecyclerHolder holder, WorkPlandetailInfo.DataBean.PlanBean.UdataBean item, int position, boolean isScrolling) {
                                    holder.setImageByUrl(R.id.iv_img, item.getImg());
                                    holder.setText(R.id.tv_uname, item.getUname());
                                }
                            };
                            rvUdate.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                            rvUdate.setAdapter(adapter);

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 1.无合作意向进行跟进
     * 2.有合作意向上传证书
     * 3.全部展开
     *
     * @param view
     */
    @OnClick({R.id.bt_nohezuo, R.id.bt_yeshezuo, R.id.iv_qbzk})
    public void onViewClicked(View view) {
        if (OnClickFlagUtil.isFastClick()) {
            return;
        }
        switch (view.getId()) {

            case R.id.bt_nohezuo:


                postWorkPlanlist("2");

                break;
            case R.id.bt_yeshezuo:
                postWorkPlanlist("1");
                break;
            case R.id.iv_qbzk:
                break;
        }
    }


}
